home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / LINE.C < prev    next >
C/C++ Source or Header  |  1993-05-27  |  4KB  |  150 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 05-24-93 (18:30)             Number: 22
  4. From: BRIAN DESSENT                Refer#: 35
  5.   To: DAVID MOHORN                  Recvd: NO  
  6. Subj: Line-drawing method            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. Hello David...
  9.  
  10. Sunday May 23 1993, David Mohorn writes to All:
  11.  
  12.  DM> Does anyone know Bresenham's Algorithm for his line-drawing method?
  13.  
  14. /* Public Domain mode 13h Bresenham line/circle algorithms */
  15. /* By Brian Dessent */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #include <time.h>
  21. #include <conio.h>
  22.  
  23. #define SCREEN_WIDTH 320
  24. #define SCREEN_HEIGTH 200
  25. #define MAX_X 319
  26. #define MAX_Y 199
  27.  
  28. /* prototypes */
  29.  
  30. void setmode(int mode);
  31. void plotdot(int x, int y, char c);
  32. void bresenham_line(int x, int y, int x2, int y2, char c);
  33. void bresenham_circle(int xc, int yc, int r, char c);
  34. void main(void);
  35.  
  36. /* code begins */
  37.  
  38. /* uses BIOS to set video mode */
  39. void setmode(int mode)
  40. {
  41.   union REGS r;
  42.  
  43.   r.x.ax = mode;
  44.   int86(0x10, &r, &r);
  45. }
  46.  
  47. /* plots a dot at (x, y) with color c */
  48. void plotdot(int x, int y, char c)
  49. {
  50.   register char far *addr;
  51.  
  52.   if(x < 0 || x > MAX_X || y < 0 || y > MAX_Y)
  53.     return;
  54.  
  55.   addr = MK_FP(0xa000, (SCREEN_WIDTH * y) + x);
  56.   *addr = c;
  57. }
  58.  
  59.  
  60. /* draws a line from (x, y) to (x2, y2) in color c */
  61. void bresenham_line(int x, int y, int x2, int y2, char c)
  62. {
  63.   int i, steep = 0, sx, sy, dx, dy, e;
  64.  
  65.   dx = abs(x2 - x);
  66.   sx = ((x2 - x) > 0) ? 1 : -1;
  67.   dy = abs(y2 - y);
  68.   sy = ((y2 - y) > 0) ? 1 : -1;
  69.  
  70.   if(dy > dx) {
  71.     steep = 1;
  72.     x ^= y;  /* swap x and y */
  73.     y ^= x;
  74.     x ^= y;
  75.     dx ^= dy; /* swap dx and dy */
  76.     dy ^= dx;
  77.     dx ^= dy;
  78.     sx ^= sy; /* swap sx and sy */
  79.     sy ^= sx;
  80.     sx ^= sy;
  81.   }
  82.  
  83.   e = 2 * dy - dx;
  84.   for(i = 0;i < dx;i++) {
  85.     if(steep)
  86.       plotdot(y, x, c);
  87.     else
  88.       plotdot(x, y, c);
  89.     while(e >= 0) {
  90.       y += sy;
  91.       e -= 2 * dx;
  92.     }
  93.     x += sx;
  94.     e += 2 * dy;
  95.   }
  96.   plotdot(x2, y2, c);
  97. }
  98.  
  99.  
  100. /* draws a circle at (xc, yc) with radius r in color c */
  101. /* note: the scaling factor of (SCREEN_WIDTH / SCREEN_HEIGTH) is used when
  102. updating d.  This makes round circles.  If you want ellipses, you can modify
  103. that ratio. */
  104. void bresenham_circle(int xc, int yc, int r, char c)
  105. {
  106.   int x = 0, y = r, d = 2 * (1 - r);
  107.  
  108.   while(y > 0) {
  109.     plotdot(xc + x, yc + y, c);
  110.     plotdot(xc + x, yc - y, c);
  111.     plotdot(xc - x, yc + y, c);
  112.     plotdot(xc - x, yc - y, c);
  113.     if(d + y > 0) {
  114.       y -= 1;
  115.       d -= (2 * y * SCREEN_WIDTH / SCREEN_HEIGTH) - 1;
  116.     }
  117.     if(x > d) {
  118.       x += 1;
  119.       d += (2 * x) + 1;
  120.     }
  121.   }
  122. }
  123.  
  124. /* draws random lines and circles until a key is pressed in mode 13h */
  125. /* (draws in colors 0 - 63 only) */
  126. void main(void)
  127. {
  128.   int i=0;
  129.  
  130.   randomize();
  131.   setmode(0x13);
  132.   while(!kbhit()) {
  133.     bresenham_line(random(SCREEN_WIDTH), random(SCREEN_HEIGTH),
  134.         random(SCREEN_WIDTH), random(SCREEN_HEIGTH), ++i % 64);
  135.     bresenham_circle(random(SCREEN_WIDTH), random(SCREEN_HEIGTH),
  136.         random(50), ++i % 64);
  137.   }
  138.   getch();
  139.   setmode(0x03);  /* set to color text mode, clearing screen */
  140. }
  141.  
  142.  
  143. Brian
  144.  
  145. --- FMail 0.94
  146.  * Origin: Brian's Board (1:3641/202)
  147. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  148. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 261/1023
  149. SEEN-BY: 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  150.